package com.my.springboot.tuling.concurrent;




import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ScheduledThreadPoolExecutorExample {
    public static void main(String[] args) {
        ScheduledThreadPoolExecutor executor = (ScheduledThreadPoolExecutor)Executors.newScheduledThreadPool(5);
        Task task = new Task("任务");
        System.out.println("created:" + task.getName());
        executor.schedule(task,2, TimeUnit.SECONDS);
        //executor.scheduleWithFixedDelay(task,0,2,TimeUnit.SECONDS);//任务+延迟
        //executor.scheduleAtFixedRate(task,0,1,TimeUnit.SECONDS);//任务延迟取最大值 稳定定时器
    }
}

class Task implements Runnable{
    private String name;

    public Task(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public void run() {
        System.out.println("Executing" + name +",Current Seconds:" + new Date().getSeconds());
        try{
            Thread.sleep(1000);
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}
